package com.example.myapplication.tack_20.fragment;

import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;

import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.myapplication.R;
import com.example.myapplication.tack_20.adapter.ArrangeAdapter;
import com.example.myapplication.tack_20.data.ArrangeData;
import com.example.myapplication.tack_20.data.BannerData;
import com.example.myapplication.utils.BannerTool;
import com.example.myapplication.utils.OkHttpTool;

import java.util.ArrayList;
import java.util.List;


public class NewsFragment extends Fragment {
    private View view;
    private Handler handler;
    private List<ArrangeData.RowsDTO> listArrange;
    private ViewPager banner;
    private RecyclerView arrange;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.tack_20_fragment_news, container, false);
        init();
        getBanner();
        getArrange();
        return view;
    }

    private void getArrange() {
        String url = "prod-api/api/volunteer-service/news/list";
        OkHttpTool.doNetRequest(url, OkHttpTool.GET, ArrangeData.class, new OkHttpTool.NetCallback() {
            @Override
            public void onSuccess(Object obj) {
                ArrangeData arrange = (ArrangeData)obj;
                if(arrange.getCode() == 200){

                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            listArrange = arrange.getRows();
                            ArrAdapter();
                        }
                    });
                }
            }

            @Override
            public void onFailure(String msg) {

            }
        });
    }

    private void ArrAdapter() {
        ArrangeAdapter arrangeAdapter = new ArrangeAdapter(listArrange, NewsFragment.this.getContext());
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(NewsFragment.this.getContext());
        arrange.setAdapter(arrangeAdapter);
        arrange.setLayoutManager(linearLayoutManager);
    }

    private void getBanner() {
        String url = "prod-api/api/volunteer-service/ad-banner/list";
        OkHttpTool.doNetRequest(url, OkHttpTool.GET, BannerData.class, new OkHttpTool.NetCallback() {
            @Override
            public void onSuccess(Object obj) {
                BannerData homeBanner = (BannerData)obj;
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        ArrayList<String> list = new ArrayList<>();
                        for (BannerData.DataDTO rowsDTO :  homeBanner.getData()) {
                            list.add(rowsDTO.getImgUrl());
                        }
                        BannerTool bannerTool = new BannerTool(NewsFragment.this.getContext(),list,banner);
                        banner.setAdapter(bannerTool);
                    }
                });


            }

            @Override
            public void onFailure(String msg) {

            }
        });
    }




    private void init() {
        handler = new Handler();
        arrange = view.findViewById(R.id.arrange);
        banner = view.findViewById(R.id.banner);
    }
}